package com.mf.morefun.adapter;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.WordBean;
import com.mf.morefun.listener.OnStartDragListener;

import java.util.List;

/**
 * Created by Administrator on 2017/3/13 0013.
 * E-Mail：543441727@qq.com
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<WordBean> datas;
    private Context mContext;
    private LayoutInflater mLiLayoutInflater;
    private OnStartDragListener mDragStartListener;

    public OnStartDragListener getmDragStartListener() {
        return mDragStartListener;
    }

    public void setmDragStartListener(OnStartDragListener mDragStartListener) {
        this.mDragStartListener = mDragStartListener;
    }

    public MyAdapter(List<WordBean> datas, Context context) {
        this.datas = datas;
        this.mContext = context;
        this.mLiLayoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLiLayoutInflater.inflate(R.layout.item_linear, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (datas.get(position).isBg()){
            holder.ll_item.setBackgroundResource(R.drawable.white_shape);
        }else {
            holder.ll_item.setBackgroundColor(0);
        }
        holder.img.setImageResource(datas.get(position).getImageId());
        if (datas.get(position).isSure()){
            holder.img.setVisibility(View.VISIBLE);
        }else {
            holder.img.setVisibility(View.INVISIBLE);
        }
        if (position >= (datas.size() / 2)){
            holder.ll_item.setBackgroundColor(0);
        }
        holder.ll_item.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;
        public LinearLayout ll_item;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.iv_word);
            ll_item = itemView.findViewById(R.id.ll_word);
        }
    }
}
